package com.xuyh.pdf.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xuyh
 * pdf转成图片
 */
public class PDF2Photo {
    private final static String path = "c:\\xuyh\\2\\";
    private final static String targetPath = "c:\\xuyh\\3\\";
    private final static JSONArray result = new JSONArray();
    /**
     * 图片清晰度设置
     * pdf大，dpi设置75，pdf小，dpi设置大一些 150 225 300 等
     */
    private final static Integer dpi = 150;

    public static void main(String[] args) throws Exception {
        List<String> listFileNames = FileUtil.listFileNames(path);

        for (String fileName : listFileNames) {
            generate(fileName);
        }
        String jsonString = result.toJSONString(2);
        IoUtil.write(new FileOutputStream(targetPath + "json.txt"), true, jsonString.getBytes());
        System.out.println(jsonString);
    }

    private static void generate(String fileName) {
        try {
            JSONObject tmp = new JSONObject();
            result.add(tmp);
            tmp.set("name", fileName);
            tmp.set("sort", 0);

            // 加载PDF文档
            File file = new File(path, fileName);
            String parentPath = targetPath + StrUtil.replace(fileName, ".pdf", StrUtil.EMPTY);
            // 创建文件夹
            FileUtil.mkdir(parentPath);
            Integer count = doGenerate(file, parentPath);
            tmp.set("count", count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Integer doGenerate(File file, String parentPath) throws IOException {
        PDDocument document = Loader.loadPDF(file);
        int numberOfPages = document.getNumberOfPages();
        doDocument(document, parentPath);

        return numberOfPages;
    }

    public static Integer doGenerate(InputStream inputStream, String parentPath) throws IOException {
        PDDocument document = Loader.loadPDF(inputStream);
        int numberOfPages = document.getNumberOfPages();
        doDocument(document, parentPath);
        return numberOfPages;
    }

    public static void doDocument(PDDocument document, String parentPath) throws IOException {
        int numberOfPages = document.getNumberOfPages();
        // 创建PDFRenderer对象
        PDFRenderer renderer = new PDFRenderer(document);
        // 遍历每个页面，并将其转换为图片
        for (int i = 0; i < numberOfPages; i++) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            BufferedImage image = renderer.renderImageWithDPI(i, dpi, ImageType.RGB);
            // 图片截取
            image = image.getSubimage(240, 240, 750, 1200);

            // 保存图片到文件
            ImageIO.write(image, "jpg", outputStream);
            // 转换数据
            image = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));
            ImageIO.write(image, "webp", outputStream);
            IoUtil.write(new FileOutputStream(new File(parentPath, i + 1 + ".webp")), true, outputStream.toByteArray());

        }
        // 关闭文档
        document.close();
    }
}